<?php namespace App\Http\Controllers\Api\Admin;

use App\Banner;
use App\Http\Controllers\Controller;
use Auth;
use App\Product;
use App\ProductImgs;
use App\Classify;
use App\Admin;
use App\HomeImg;
use App\User;
use Illuminate\Http\Request;

class SystemController extends Controller
{
    public function setBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title   = $request->get('title');
        $linkUrl = $request->get('link_url');
        $sort    = $request->get('sort');
        $imgId   = $request->get('img_id');
        $id      = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBanner = Banner::whereId(trim($id))->first();

        if(!$thatBanner)
            $thatBanner = new Banner();

        if(!is_null($title))$thatBanner->title = $title;
        if(!is_null($linkUrl))$thatBanner->link_url = $linkUrl;
        if(!is_null($sort))$thatBanner->sort = $sort;
        if(!is_null($imgId))$thatBanner->img_id = $imgId;
        $thatBanner->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$thatBanner]);
    }

    public function delBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Banner $thatBanner */
        $thatBanner = Banner::whereId($id)->first();

        if(!$thatBanner)
            return response()->json(["level"=>"error","message"=>"该banner不存在！"]);

        $thatBanner->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatBanner]);
    }

    public function addClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $name = $request->get("name");
        $type = $request->get('pid');
        $sort = $request->get('sort');
        $id      = !is_null($request->get('id'))?$request->get('id'):0;

        $newClassify = classify::whereId(trim($id))->first();

        if(!$newClassify)
            $newClassify = new classify();

        $newClassify->type = $type;
        $newClassify->name = $name;
        $newClassify->sort = $sort;
        $newClassify->save();


        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newClassify]);
    }

    public function delClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Classify $thatClassify */
        $thatClassify = Classify::whereId($id)->first();

        if(!$thatClassify)
            return response()->json(["level"=>"error","message"=>"该分类不存在！"]);

        $thatClassify->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatClassify]);
    }

    public function setProduct(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $content         = $request->get("content");
        $video_id        = $request->get('video_id');
        $imgIdsString    = $request->get('img_ids');
        $classify_one    = $request->get('classify_one');
        $classify_two    = $request->get('classify_two');
        $classify_three  = $request->get('classify_three');
        $status          = $request->get('status');

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

//        if($video_iis_null($imgIdsString))
//            return response()->json(['level'=>'error','message' => '图片不能为空']);

        $newProduct = Product::whereId(trim($id))->first();
        if(!$newProduct)
            $newProduct = new Product();

        if(!is_null($content))$newProduct->content = $content;
        if(!is_null($video_id))$newProduct->video_id = $video_id;
        if(!is_null($classify_one))$newProduct->classify_one = $classify_one;
        if(!is_null($classify_two))$newProduct->classify_two = $classify_two;
        if(!is_null($classify_three))$newProduct->classify_three = $classify_three;
        if(!is_null($status))$newProduct->status = $status;
        $newProduct->save();

        $oldImgs = ProductImgs::wherePId($newProduct->id)->get();
        if(count($oldImgs) > 0){
            foreach ($oldImgs as $oldImg){
                /** @var ProductImgs $oldImg */
                $oldImg->forceDelete();
            }
        }

        $imgIds = explode(",",$imgIdsString);
        if(count($imgIds) > 0){
            foreach ($imgIds as $imgId){
                $newProductImg = new ProductImgs();
                $newProductImg->img_id = $imgId;
                $newProductImg->p_id   = $newProduct->id;
                $newProductImg->save();
            }
        }

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newProduct]);
    }

    public function delProduct(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Classify $thatClassify */
        $thatClassify = Product::whereId($id)->first();

        if(!$thatClassify)
            return response()->json(["level"=>"error","message"=>"该产品不存在！"]);

        $thatClassify->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatClassify]);
    }

    public function setProductImgs(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $pId = $request->get('p_id');
        $imgId = $request->get('img_id');
        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $newProductImgs = ProductImgs::whereId(trim($id))->first();

        if(!$newProductImgs)
            $newProductImgs = new ProductImgs();

        if(!is_null($pId))$newProductImgs->p_id = $pId;
        if(!is_null($imgId))$newProductImgs->img_id = $imgId;
        $newProductImgs->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newProductImgs]);
    }

    public function delProductImgs(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Classify $thatClassify */
        $thatClassify = ProductImgs::whereId($id)->first();

        if(!$thatClassify)
            return response()->json(["level"=>"error","message"=>"该图片不存在！"]);

        $thatClassify->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatClassify]);
    }

    public function setAdmin(Request $request){
    if(Auth::guard("admin")->guest())
        return response()->json(['level'=>'error','message' => '请先登录！']);

    $username = $request->get('username');
    $password = $request->get('password');
    $repass = $request->get('repass');

    if($password != $repass)
        return response()->json(['level'=>'error','message' => '两次密码不一致！']);

    /** @var Admin $hasUser */
    $hasUser = Admin::whereUsername($username)->first();
    if($hasUser){
        return response()->json(['level'=>'error','message' => '当前管理员账号已存在']);
    }

    $thatAdmin = new Admin();
    $thatAdmin->username = $username;
    $thatAdmin->password = bcrypt($password);
    $thatAdmin->status = 2;
    $thatAdmin->save();

    return response()->json(['level'=>'success','message'=>'添加成功','data'=>$thatAdmin]);
}

    public function delAdmin(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Admin $thatAdmin */
        $thatAdmin = Admin::whereId($id)->first();

        if(!$thatAdmin)
            return response()->json(["level"=>"error","message"=>"该管理员不存在！"]);

        if($thatAdmin->status == 1)
            return response()->json(["level"=>"error","message"=>"该管理员不能删除！"]);


        $thatAdmin->forceDelete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatAdmin]);
    }

    public function setHomeImg(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $imgId = $request->get('img_id');
        $id = $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatHomeImg = HomeImg::whereId(trim($id))->first();

        if(!$thatHomeImg)
            $thatHomeImg = new HomeImg();

        if(!is_null($imgId))$thatHomeImg->img_id = $imgId;
        $thatHomeImg ->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$thatHomeImg]);
    }

    public function changeUserStatus(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $status = $request->get('status');
        $id = $request->get('id');

        $changeStatus = User::whereId($id)->first();

        if(!$changeStatus)
            return response()->json(["level"=>"error","message"=>"该用户不存在！"]);

        if($changeStatus->status == 1){
            $changeStatus->status = 0;
        }else{
            $changeStatus->status = 1;
        }

        $changeStatus->save();

        return response()->json(['level'=>'success','message'=>'状态修改成功','data'=>$changeStatus]);
    }
}